package org.example.ymcc.producer;

import com.alibaba.druid.sql.visitor.functions.Concat;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.example.ymcc.constant.Constants;
import org.example.ymcc.constant.MQConstants;
import org.example.ymcc.domain.CourseOrder;
import org.example.ymcc.domain.CourseOrderItem;
import org.example.ymcc.dto.OrderDto;
import org.example.ymcc.service.ICourseOrderItemService;
import org.example.ymcc.service.ICourseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.util.List;

//保存课程订单以及支付订单的事务消息
@Component
@Slf4j
@RocketMQTransactionListener(
        txProducerGroup = MQConstants.MQ_COURSE_ORDER_PAY_GROUP_TRANSACTION
)
public class CourseOrderTransactionListener implements RocketMQLocalTransactionListener {
    @Autowired
    private ICourseOrderService courseOrderService;
    @Autowired
    private ICourseOrderItemService courseOrderItemService;

    /**
     * 执行本地事务
     * @param message
     * @param o
     * @return
     */
    @Override
    @Transactional
    public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object o) {
        try {
            OrderDto order = (OrderDto)o;
            //执行本地事务
            courseOrderService.save(order.getCourseOrder());
            List<CourseOrderItem> courseOrderItems = order.getCourseOrderItems();
            courseOrderItems.forEach(item->{
                item.setOrderId(order.getCourseOrder().getId());
            });
            courseOrderItemService.saveBatch(courseOrderItems);
        }catch (Exception e){
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        return RocketMQLocalTransactionState.COMMIT;
    }

    /**
     * 检查本地事务
     * @param message
     * @return
     */
    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
        try {
            byte[] bytes = (byte[])message.getPayload();
            String s = new String(bytes, Constants.UTF8);
            CourseOrder courseOrder = JSONObject.parseObject(s, CourseOrder.class);
            String orderNo = courseOrder.getOrderNo();//得到订单号
            //如果在这个根据订单编号去查询  找不到数据？
            CourseOrder one =
                    courseOrderService.getOne(new LambdaQueryWrapper<CourseOrder>().eq(CourseOrder::getOrderNo, orderNo));
            if(one != null){
                return RocketMQLocalTransactionState.COMMIT;
            }
        } catch (UnsupportedEncodingException e) {
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        return RocketMQLocalTransactionState.ROLLBACK;
    }
}
